package org.jeecg.modules.third.ecloud.sdk;

import com.alibaba.fastjson.JSONObject;
import org.jeecg.common.api.vo.Result;
import org.jeecg.common.util.DateUtils;
import org.jeecg.modules.third.ecloud.sdk.bean.ECloudDomain;
import org.jeecg.modules.third.ecloud.sdk.http.EcloudClient;
import org.jeecg.modules.third.ecloud.sdk.util.DateUtil;
import org.jeecg.modules.wlhy.company.entity.HyCompany;
import org.jeecg.modules.wlhy.customer.entity.HyCustomer;
import org.jeecg.modules.wlhy.customer.service.IHyCustomerService;
import org.jeecg.modules.wlhy.enterprise.entity.HyEnterprise;
import org.jeecg.modules.wlhy.utils.HyConfigUtils;
import org.jeecg.modules.wlhy.hydriver.entity.HyDriver;
import org.jeecg.modules.wlhy.hyvehicle.entity.HyVehicle;
import org.jeecg.modules.wlhy.shipping.entity.HyShippingNote;
import org.jeecg.modules.wlhy.utils.CodeUtil;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;

import java.io.File;
import java.util.ArrayList;
import java.util.List;

@Service
public class EcloudSdkService {

    private final Logger logger = LoggerFactory.getLogger(EcloudSdkService.class);

    @Value(value = "${jeecg.path.upload}")
    private String uploadpath;

//    @Value(value = "${mabang-contract.url}")
//    private String url;
//
//    @Value(value = "${mabang-contract.appKey}")
//    private String appKey;
//
//    @Value(value = "${mabang-contract.secrept}")
//    private String secrept;

//    @Value(value = "${mabang-contract.templateNumber}")
    private String templateNumber;

    //文件保存地址路径
    private String SAVE_CONTRACT_ADDR = "documents" + File.separator +  DateUtils.getDate("yyyyMMdd")+ File.separator;

    private String PATH_SUBFIX;

    //运输协议文件名前缀
    private String SHIPPING_NAME_SUBFIX= "运输协议_";

    //PDF后缀
    private String PDF_SUFFIX = ".pdf";

    @Autowired
    private IHyCustomerService hyCustomerService;

    public Result autoSign(HyDriver driver, HyCompany hyEnterprise, HyVehicle vehicle, HyShippingNote shippingNote) {
        PATH_SUBFIX = uploadpath + File.separator ;

        templateNumber= HyConfigUtils.getConfig().getYyzContractTemplatenumber();

        HyCustomer Customer = hyCustomerService.getById(shippingNote.getCustomerId());

        String customerName = Customer.getCustomerName();

        String qiyexinxi=null;

        if(shippingNote.getOrderMode().equals("1")){
            qiyexinxi=hyEnterprise.getCompanyName();
        }else{
            qiyexinxi=customerName;
        }

        //初始化应用配置
//        EcloudPublicKey.init(appKey, secrept, "1.0", url);


        //生成证书  司机
        ECloudDomain s1 = EcloudClient.applyCert("1", "0", driver.getIdcard(), driver.getDriverName(), driver.getTelephone());

//        logger.info("s1"+JSONObject.toJSONString(s1));
        //创建印章 司机
        ECloudDomain s3 = EcloudClient.createSign(driver.getTelephone(), driver.getDriverName(), 1, 32, 2, "#FF0000");
//        logger.info("s3"+JSONObject.toJSONString(s3));
        JSONObject resultStr = (JSONObject) JSONObject.parse(s3.getData().toString());

        //查询印章图片 司机
        ECloudDomain s5 = EcloudClient.getSignImg(driver.getTelephone(), resultStr.getString("signId"));
        JSONObject resultStr5 = (JSONObject) JSONObject.parse(s5.getData().toString());
        //添加签名/印章 司机
        ECloudDomain s6 = EcloudClient.addSign(driver.getTelephone(), "1", resultStr5.getString("signImg"));
//        logger.info("s6"+JSONObject.toJSONString(s6));
        //添加签名/印章 signId会变  需要重新赋值


        //生成证书  企业
        ECloudDomain s2 = EcloudClient.applyCert("2", "0", hyEnterprise.getFddbrsfzjhm(), hyEnterprise.getCompanyName(), hyEnterprise.getLegalerTel());
//        logger.info("s2"+JSONObject.toJSONString(s2));
        //创建印章 企业
        ECloudDomain s4 = EcloudClient.createSeal(hyEnterprise.getLegalerTel(), qiyexinxi, "", "");
//        logger.info("s4"+JSONObject.toJSONString(s4));
        JSONObject resultStrs4 = (JSONObject) JSONObject.parse(s4.getData().toString());
        //查询印章图片 企业
        ECloudDomain s7 = EcloudClient.getSignImg(hyEnterprise.getLegalerTel(), resultStrs4.getString("signId"));
        JSONObject resultStrs7 = (JSONObject) JSONObject.parse(s7.getData().toString());
        //添加签名/印章 企业
        ECloudDomain s8 = EcloudClient.addSign(hyEnterprise.getLegalerTel(), "2", resultStrs7.getString("signImg"));
//        logger.info("s8"+JSONObject.toJSONString(s8));

        //根据模板生成合同文档
        JSONObject o = new JSONObject();
        o.put("mobilePhone", hyEnterprise.getLegalerTel());
        o.put("templateNumber", templateNumber);
        o.put("name", "模板");

        List<JSONObject> jsonObjects = new ArrayList<>();

        String contractNum = CodeUtil.createContractNum();

        //合同号
        JSONObject zj = new JSONObject();
        zj.put("contextId", "fdbba0398674c428");
        zj.put("contextValue", contractNum);
        jsonObjects.add(zj);

        //甲方
        JSONObject zj1 = new JSONObject();
        zj1.put("contextId", "3b493cd1ca051c1d");
        zj1.put("contextValue", qiyexinxi);
        jsonObjects.add(zj1);
        //乙方
        JSONObject zj2 = new JSONObject();
        zj2.put("contextId", "bab3281b99745f23");
        zj2.put("contextValue",driver.getDriverName());
        jsonObjects.add(zj2);

        //货物运输
        JSONObject huow = new JSONObject();
        huow.put("contextId", "707bd0cd63907d00");
        huow.put("contextValue","货物运输");
        jsonObjects.add(huow);
        String time = DateUtil.getDay();
        //提货时间
        JSONObject zj3 = new JSONObject();
        zj3.put("contextId", "a6566b20f233ce26");
        if(shippingNote.getReceiveDate()==null){
            zj3.put("contextValue","-");
        }else{
            zj3.put("contextValue",DateUtil.fomatString2(shippingNote.getReceiveDate()) );

        }
        jsonObjects.add(zj3);
        //起运地
        JSONObject zj5 = new JSONObject();
        zj5.put("contextId", "d067e68b983e2460");
        zj5.put("contextValue", shippingNote.getSendAddressName());
        jsonObjects.add(zj5);

        //目的地
        JSONObject zj6 = new JSONObject();
        zj6.put("contextId", "1190c6cd7c79e8d3");
        zj6.put("contextValue", shippingNote.getReceiveAddressName());
        jsonObjects.add(zj6);
        //承运人
        JSONObject zj7 = new JSONObject();
        zj7.put("contextId", "8f310d0ab8800395");
        zj7.put("contextValue", driver.getDriverName());
        jsonObjects.add(zj7);
        //联系方式
        JSONObject zj8 = new JSONObject();
        zj8.put("contextId", "90db4ebbd9189855");
        zj8.put("contextValue", driver.getTelephone());
        jsonObjects.add(zj8);
        //车牌号
        JSONObject zj9 = new JSONObject();
        zj9.put("contextId", "7e2787f2f01d2a63");
        zj9.put("contextValue", vehicle.getVehicleNumber());
        jsonObjects.add(zj9);
        //货物名称
        JSONObject zj10 = new JSONObject();
        zj10.put("contextId", "6d8b9b64c9ad3e08");
        zj10.put("contextValue", shippingNote.getDescriptionOfGoods());
        jsonObjects.add(zj10);
        //计划数量
        JSONObject zj11 = new JSONObject();
        zj11.put("contextId", "59b038fd8f673822");
        zj11.put("contextValue", vehicle.getVehicleTonnage());
        jsonObjects.add(zj11);
        //运费金额
        if(shippingNote.getCostPayShipping()==null){
            JSONObject zj12 = new JSONObject();
            zj12.put("contextId", "c553ea4dc05cead2");
            zj12.put("contextValue", shippingNote.getShippingUnitPrice());
            jsonObjects.add(zj12);
        }else{
            JSONObject zj12 = new JSONObject();
            zj12.put("contextId", "c553ea4dc05cead2");
            zj12.put("contextValue", shippingNote.getCostPayShipping());
            jsonObjects.add(zj12);
        }
        //现金支付金额
        JSONObject zj13 = new JSONObject();
        zj13.put("contextId", "7c9b93c7beb829bd");
        zj13.put("contextValue", "0");
        jsonObjects.add(zj13);
        //燃料费用
        JSONObject zj14 = new JSONObject();
        zj14.put("contextId", "f105110d0cf2e2db");
        zj14.put("contextValue", "0");
        jsonObjects.add(zj14);
        //ETC 费用
        JSONObject zj15 = new JSONObject();
        zj15.put("contextId", "423490bb2d48b339");
        zj15.put("contextValue", "0");
        jsonObjects.add(zj15);

        //提货时间
        JSONObject zj16 = new JSONObject();
        zj16.put("contextId", "6ec380c9e84d4813");
        if(shippingNote.getSendDate()==null){
            zj16.put("contextValue", "-");
        }else{
            zj16.put("contextValue", DateUtil.fomatString2(shippingNote.getStatusDtLoading()) );
        }
        jsonObjects.add(zj16);
        //货物送达交付时间
        JSONObject zj17 = new JSONObject();
        zj17.put("contextId", "420db522370be39d");
        zj17.put("contextValue", "-");

        jsonObjects.add(zj17);
        //合同签订时间
        JSONObject zj18 = new JSONObject();
        zj18.put("contextId", "be8da64ae1dc989c");


        if(shippingNote.getSendDate()==null){
            zj18.put("contextValue","-");
        }else{
            zj18.put("contextValue", DateUtil.fomatString2(shippingNote.getStatusDtLoading()) );
        }
        jsonObjects.add(zj18);


        //最后两个时间
        JSONObject zj19 = new JSONObject();
        zj19.put("contextId", "328510c8e72632c3");
        zj19.put("contextValue", time);
        jsonObjects.add(zj19);
        //最后两个时间
        JSONObject zj20 = new JSONObject();
        zj20.put("contextId", "6f6ce5a0d6cb6a7e");
        zj20.put("contextValue", time);
        jsonObjects.add(zj20);

        o.put("simpleFormFields", jsonObjects);

        ECloudDomain s = EcloudClient.createByTemplateComponents(JSONObject.toJSONString(o));
//        logger.info("s"+JSONObject.toJSONString(o));
        JSONObject hetong = (JSONObject) JSONObject.parse(s.getData().toString());
//        logger.info("hetong"+JSONObject.toJSONString(hetong));


        //自动签字 司机
        JSONObject o2 = new JSONObject();
        o2.put("type", "1");
        o2.put("cardType", "0");
        o2.put("idCardNum", driver.getIdcard());
        o2.put("name", driver.getDriverName());
        o2.put("mobilePhone",driver.getTelephone());
        JSONObject o3 = new JSONObject();
        o3.put("signType", "1");
        o3.put("signId", resultStr.getString("signId"));
        o3.put("positionName", "siji");

        ECloudDomain s123 = EcloudClient.autoSign(JSONObject.toJSONString(o2), hetong.getString("contractNum"), 1, JSONObject.toJSONString(o3));
//        logger.info("s123"+JSONObject.toJSONString(s123));
        //自动签字 企业
        JSONObject oq = new JSONObject();
        oq.put("type", "2");
        oq.put("cardType", "0");
        oq.put("idCardNum", hyEnterprise.getFddbrsfzjhm());
        oq.put("name", qiyexinxi);
        oq.put("mobilePhone", hyEnterprise.getLegalerTel());
        JSONObject o2q = new JSONObject();
        o2q.put("signType", "2");
        o2q.put("signId", resultStrs4.getString("signId"));
        o2q.put("positionName", "qiye");

        ECloudDomain sq = EcloudClient.autoSign(JSONObject.toJSONString(oq), hetong.getString("contractNum"), 0, JSONObject.toJSONString(o2q));
//        logger.info("sq"+JSONObject.toJSONString(sq));

        String path = PATH_SUBFIX + SAVE_CONTRACT_ADDR;
        File fileDir = new File(path);
        if(!fileDir.exists()) {
            fileDir.mkdirs();//创建目录
        }
        String contractPath = SAVE_CONTRACT_ADDR + SHIPPING_NAME_SUBFIX + DateUtils.getDate("yyyyMMddHHmmss") + PDF_SUFFIX;

//        String filename = PATH_SUBFIX + contractPath;


        //下载合同  等待点击查看时下载合同
//        ECloudDomain sx = EcloudClient.downloadCont(hetong.getString("contractNum"), hyEnterprise.getLegalerTel(), filename);


        JSONObject he = new JSONObject();
//        he.put("contractPath",contractPath);
        he.put("contractNum",hetong.getString("contractNum"));
        he.put("contractNum2",contractNum);
        return Result.OK(he);
    }


    public Result autoSign2(HyDriver driver, HyEnterprise hyEnterprise,  HyVehicle vehicle, HyShippingNote shippingNote) {
        PATH_SUBFIX = uploadpath + File.separator ;

        HyCustomer Customer = hyCustomerService.getById(shippingNote.getCustomerId());

        String customerName = Customer.getCustomerName();

        String qiyexinxi=null;

        if(shippingNote.getOrderMode().equals("1")){
            qiyexinxi=hyEnterprise.getEntName();
        }else{
            qiyexinxi=customerName;
        }

        //初始化应用配置
//        EcloudPublicKey.init(appKey, secrept, "1.0", url);


        //生成证书  司机
        ECloudDomain s1 = EcloudClient.applyCert("1", "0", driver.getIdcard(), driver.getDriverName(), driver.getTelephone());

//        logger.info("s1"+JSONObject.toJSONString(s1));
        //创建印章 司机
        ECloudDomain s3 = EcloudClient.createSign(driver.getTelephone(), driver.getDriverName(), 1, 32, 2, "#FF0000");
//        logger.info("s3"+JSONObject.toJSONString(s3));
        JSONObject resultStr = (JSONObject) JSONObject.parse(s3.getData().toString());

        //查询印章图片 司机
        ECloudDomain s5 = EcloudClient.getSignImg(driver.getTelephone(), resultStr.getString("signId"));
        JSONObject resultStr5 = (JSONObject) JSONObject.parse(s5.getData().toString());
        //添加签名/印章 司机
        ECloudDomain s6 = EcloudClient.addSign(driver.getTelephone(), "1", resultStr5.getString("signImg"));
//        logger.info("s6"+JSONObject.toJSONString(s6));
        //添加签名/印章 signId会变  需要重新赋值


        //生成证书  企业
        ECloudDomain s2 = EcloudClient.applyCert("2", "0", hyEnterprise.getFddbrsfzjhm(), hyEnterprise.getLegalerName(), hyEnterprise.getLegalerTel());
//        logger.info("s2"+JSONObject.toJSONString(s2));
        //创建印章 企业
        ECloudDomain s4 = EcloudClient.createSeal(hyEnterprise.getLegalerTel(), qiyexinxi, "", "");
//        logger.info("s4"+JSONObject.toJSONString(s4));
        JSONObject resultStrs4 = (JSONObject) JSONObject.parse(s4.getData().toString());
        //查询印章图片 企业
        ECloudDomain s7 = EcloudClient.getSignImg(hyEnterprise.getLegalerTel(), resultStrs4.getString("signId"));
        JSONObject resultStrs7 = (JSONObject) JSONObject.parse(s7.getData().toString());
        //添加签名/印章 企业
        ECloudDomain s8 = EcloudClient.addSign(hyEnterprise.getLegalerTel(), "2", resultStrs7.getString("signImg"));
//        logger.info("s8"+JSONObject.toJSONString(s8));

        //根据模板生成合同文档
        JSONObject o = new JSONObject();
        o.put("mobilePhone", hyEnterprise.getLegalerTel());
        o.put("templateNumber", templateNumber);
        o.put("name", "模板");

        List<JSONObject> jsonObjects = new ArrayList<>();

        String contractNum = CodeUtil.createContractNum();

        //合同号
        JSONObject zj = new JSONObject();
        zj.put("contextId", "fdbba0398674c428");
        zj.put("contextValue", contractNum);
        jsonObjects.add(zj);

        //甲方
        JSONObject zj1 = new JSONObject();
        zj1.put("contextId", "3b493cd1ca051c1d");
        zj1.put("contextValue", qiyexinxi);
        jsonObjects.add(zj1);
        //乙方
        JSONObject zj2 = new JSONObject();
        zj2.put("contextId", "bab3281b99745f23");
        zj2.put("contextValue",driver.getDriverName());
        jsonObjects.add(zj2);

        //货物运输
        JSONObject huow = new JSONObject();
        huow.put("contextId", "707bd0cd63907d00");
        huow.put("contextValue","货物运输");
        jsonObjects.add(huow);

        //提货时间
        JSONObject zj3 = new JSONObject();
        zj3.put("contextId", "a6566b20f233ce26");
        if(shippingNote.getReceiveDate()==null){
            zj3.put("contextValue","-");
        }else{
            zj3.put("contextValue",DateUtil.fomatString2(shippingNote.getReceiveDate()) );

        }
        jsonObjects.add(zj3);
        //起运地
        JSONObject zj5 = new JSONObject();
        zj5.put("contextId", "d067e68b983e2460");
        zj5.put("contextValue", shippingNote.getSendAddressName());
        jsonObjects.add(zj5);

        //目的地
        JSONObject zj6 = new JSONObject();
        zj6.put("contextId", "1190c6cd7c79e8d3");
        zj6.put("contextValue", shippingNote.getReceiveAddressName());
        jsonObjects.add(zj6);
        //承运人
        JSONObject zj7 = new JSONObject();
        zj7.put("contextId", "8f310d0ab8800395");
        zj7.put("contextValue", driver.getDriverName());
        jsonObjects.add(zj7);
        //联系方式
        JSONObject zj8 = new JSONObject();
        zj8.put("contextId", "90db4ebbd9189855");
        zj8.put("contextValue", driver.getTelephone());
        jsonObjects.add(zj8);
        //车牌号
        JSONObject zj9 = new JSONObject();
        zj9.put("contextId", "7e2787f2f01d2a63");
        zj9.put("contextValue", vehicle.getVehicleNumber());
        jsonObjects.add(zj9);
        //货物名称
        JSONObject zj10 = new JSONObject();
        zj10.put("contextId", "6d8b9b64c9ad3e08");
        zj10.put("contextValue", shippingNote.getDescriptionOfGoods());
        jsonObjects.add(zj10);
        //计划数量
        JSONObject zj11 = new JSONObject();
        zj11.put("contextId", "59b038fd8f673822");
        zj11.put("contextValue", vehicle.getVehicleTonnage());
        jsonObjects.add(zj11);
        //运费金额
        if(shippingNote.getCostPayShipping()==null){
            JSONObject zj12 = new JSONObject();
            zj12.put("contextId", "c553ea4dc05cead2");
            zj12.put("contextValue", shippingNote.getShippingUnitPrice());
            jsonObjects.add(zj12);
        }else{
            JSONObject zj12 = new JSONObject();
            zj12.put("contextId", "c553ea4dc05cead2");
            zj12.put("contextValue", shippingNote.getCostPayShipping());
            jsonObjects.add(zj12);
        }
        //现金支付金额
        JSONObject zj13 = new JSONObject();
        zj13.put("contextId", "7c9b93c7beb829bd");
        zj13.put("contextValue", "0");
        jsonObjects.add(zj13);
        //燃料费用
        JSONObject zj14 = new JSONObject();
        zj14.put("contextId", "f105110d0cf2e2db");
        zj14.put("contextValue", "0");
        jsonObjects.add(zj14);
        //ETC 费用
        JSONObject zj15 = new JSONObject();
        zj15.put("contextId", "423490bb2d48b339");
        zj15.put("contextValue", "0");
        jsonObjects.add(zj15);
        //提货时间
        JSONObject zj16 = new JSONObject();
        zj16.put("contextId", "6ec380c9e84d4813");
        if(shippingNote.getSendDate()==null){
            zj16.put("contextValue", "-" );
        }else{
            zj16.put("contextValue", DateUtil.fomatString2(shippingNote.getSendDate()) );
        }
        jsonObjects.add(zj16);
        //货物送达交付时间
        JSONObject zj17 = new JSONObject();
        zj17.put("contextId", "420db522370be39d");
        if(shippingNote.getReceiveDate()==null){
            zj17.put("contextValue", "-" );
        }else{
            zj17.put("contextValue", DateUtil.fomatString2(shippingNote.getReceiveDate()) );
        }
        jsonObjects.add(zj17);
        //合同签订时间
        JSONObject zj18 = new JSONObject();
        zj18.put("contextId", "be8da64ae1dc989c");
        String time = DateUtil.getDay();

        if(shippingNote.getSendDate()==null){
            zj18.put("contextValue", "-" );
        }else{
            zj18.put("contextValue", DateUtil.fomatString2(shippingNote.getSendDate()) );
        }
        jsonObjects.add(zj18);


        //最后两个时间
        JSONObject zj19 = new JSONObject();
        zj19.put("contextId", "328510c8e72632c3");
        zj19.put("contextValue", time);
        jsonObjects.add(zj19);
        //最后两个时间
        JSONObject zj20 = new JSONObject();
        zj20.put("contextId", "6f6ce5a0d6cb6a7e");
        zj20.put("contextValue", time);
        jsonObjects.add(zj20);

        o.put("simpleFormFields", jsonObjects);

        ECloudDomain s = EcloudClient.createByTemplateComponents(JSONObject.toJSONString(o));

        JSONObject hetong = (JSONObject) JSONObject.parse(s.getData().toString());



        //自动签字 司机
        JSONObject o2 = new JSONObject();
        o2.put("type", "1");
        o2.put("cardType", "0");
        o2.put("idCardNum", driver.getIdcard());
        o2.put("name", driver.getDriverName());
        o2.put("mobilePhone",driver.getTelephone());
        JSONObject o3 = new JSONObject();
        o3.put("signType", "1");
        o3.put("signId", resultStr.getString("signId"));
        o3.put("positionName", "zi");

        ECloudDomain s123 = EcloudClient.autoSign(JSONObject.toJSONString(o2), hetong.getString("contractNum"), 1, JSONObject.toJSONString(o3));
//        logger.info("s123"+JSONObject.toJSONString(s123));
        //自动签字 企业
        JSONObject oq = new JSONObject();
        oq.put("type", "2");
        oq.put("cardType", "0");
        oq.put("idCardNum", hyEnterprise.getFddbrsfzjhm());
        oq.put("name", qiyexinxi);
        oq.put("mobilePhone", hyEnterprise.getLegalerTel());
        JSONObject o2q = new JSONObject();
        o2q.put("signType", "2");
        o2q.put("signId", resultStrs4.getString("signId"));
        o2q.put("positionName", "zhang");

        ECloudDomain sq = EcloudClient.autoSign(JSONObject.toJSONString(oq), hetong.getString("contractNum"), 0, JSONObject.toJSONString(o2q));
//        logger.info("sq"+JSONObject.toJSONString(sq));
        String path = PATH_SUBFIX + SAVE_CONTRACT_ADDR;
        File fileDir = new File(path);
        if(!fileDir.exists()) {
            fileDir.mkdirs();//创建目录
        }
        String contractPath = SAVE_CONTRACT_ADDR + SHIPPING_NAME_SUBFIX + DateUtils.getDate("yyyyMMddHHmmss") + PDF_SUFFIX;
        String filename = PATH_SUBFIX + contractPath;


        //下载合同  等待点击查看时下载合同
//        ECloudDomain sx = EcloudClient.downloadCont(hetong.getString("contractNum"), hyEnterprise.getLegalerTel(), filename);

        JSONObject he = new JSONObject();
        he.put("contractPath",contractPath);
        he.put("contractNum",hetong.getString("contractNum"));
        he.put("contractNum2",contractNum);
        return Result.OK(he);
    }

}
